///////////////////////////////////////////////////////////////////////
// File:        EventManager.h
// Description: 
// Author:      Philip Kommnick
// Created:     Mon August 31 16:04:48 CEST 2009
//
// This program is open source. For license terms, see the LICENSE file.
//
///////////////////////////////////////////////////////////////////////

#pragma once
#include "Scanner.h"

enum TpEvents {WM_TPCLOSE=WM_USER+1, WM_WIAEVENT};

typedef struct _emEvent {
    std::wstring *pEventID;
    Scanner *pDevice;
    IUnknown *pEventInterface;
	Sink *outDest;
} emEvent;

typedef	std::vector<emEvent*> emEventList;
typedef	std::map<std::wstring, emEventList> emDeviceMap;
typedef std::map<std::wstring, emEvent*> emEventMap;

class EventManager : public IWiaEventCallback
{
public:
    // IUnknown
    virtual HRESULT __stdcall QueryInterface(REFIID riid, void **ppvObject);
    virtual ULONG __stdcall AddRef(void);
    virtual ULONG __stdcall Release(void);
    // IWiaEventCallback
    virtual HRESULT __stdcall ImageEventCallback(const GUID *pEventGUID, BSTR bstrEventDescription, BSTR bstrDeviceID,
            BSTR bstrDeviceDescription, DWORD dwDeviceType, BSTR bstrFullItemName, ULONG *pulEventType, ULONG ulReserved);
    // EventManager
    EventManager();
    virtual ~EventManager(void);
    bool PreTranslateMessage(MSG &msg);
private:
	void PreRegisterDevice(IWiaDevMgr2 *devMgr, const BSTR deviceID = NULL);
    void RegisterDevice(IWiaDevMgr2 *devMgr, const BSTR deviceID, const BSTR manufacturerName, const BSTR deviceName);
    void UnregisterDevice(const BSTR deviceID);
    static LRESULT WINAPI MsgWinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
    ULONG refCnt;
    ULONG gdiRefCnt;
    ULONG_PTR gdiplusToken;
    emEventMap eventMap;
    emDeviceMap deviceMap;
    IUnknown *deviceConnectedEvent;
    IUnknown *deviceDisconnectedEvent;
};